package com.ts.api.module.api.convert.receive.entity;

import com.gitee.apanlh.util.base.BigDecimalUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.api.ApiEnum;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.module.api.entity.bo.ApiPhoneMaskDataExchangeBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMaskField;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.List;

/**
 *  撞库返回业务消息参数
 *
 *  @author Pan
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiReceivePhoneMaskDataExchange extends ApiReceive {

    /** 请求id */
    private String reqId;
    /** 当前渠道实体 */
    private ApiKeyPO apiKeyPO;
    /** 整合过后的掩码字段 */
    private List<PlanDistributePhoneMaskField> listPhoneMaskField;

    public static ApiReceivePhoneMaskDataExchange suc() {
        ApiReceivePhoneMaskDataExchange apiReceivePhoneMd5DataExchange = new ApiReceivePhoneMaskDataExchange();
        apiReceivePhoneMd5DataExchange.setBizMsg(ApiEnum.API_SIGN_SUC.getMsg());
        apiReceivePhoneMd5DataExchange.setBizCode(ApiEnum.API_SIGN_SUC.getCode());
        return apiReceivePhoneMd5DataExchange;
    }

    public static ApiReceivePhoneMaskDataExchange fail(String msg, Integer code) {
        ApiReceivePhoneMaskDataExchange apiReceivePhoneMd5DataExchange = new ApiReceivePhoneMaskDataExchange();
        apiReceivePhoneMd5DataExchange.setBizMsg(msg);
        apiReceivePhoneMd5DataExchange.setBizCode(code);
        return apiReceivePhoneMd5DataExchange;
    }

    /**
     *  创建撞库消息业务参数
     *
     *  @param  apiPhoneMaskDataExchangeBO   API撞库实体
     *  @param  apiKeyPO                     API撞库实体
     *  @return ApiReceiveDataExchangeMessage
     */
    public static ApiReceivePhoneMaskDataExchange ofSuc(ApiPhoneMaskDataExchangeBO apiPhoneMaskDataExchangeBO, ApiKeyPO apiKeyPO) {
        ApiReceivePhoneMaskDataExchange apiReceivePhoneMaskDataExchange = suc();
        apiReceivePhoneMaskDataExchange.setApiKeyPO(apiKeyPO);
        apiReceivePhoneMaskDataExchange.setReqId(apiPhoneMaskDataExchangeBO.getReqId());

        //  设置返回字段
        List<PlanDistributePhoneMaskField> listPhoneMaskField = apiPhoneMaskDataExchangeBO.getListPhoneMaskField();
        for (PlanDistributePhoneMaskField phoneMaskField : listPhoneMaskField) {

            //  回调授权页面-如果匹配非平台则使用自己的回调授权页，如果匹配平台计划则返回的是下游授权页面
            if (ValidParam.isEmpty(phoneMaskField.getCallUrl())) {
                String h5CallUrl = H5DomainUrlUtils.createH5CallUrl(phoneMaskField.getApplyId(), apiKeyPO.getApiClientId());
                phoneMaskField.setCallUrl(h5CallUrl);
            } else {
                phoneMaskField.setCallUrl(phoneMaskField.getCallUrl());
            }

            // 渠道分成后的结算价格
            BigDecimal apiShare = apiKeyPO.getApiShare();
            if (!Eq.str("0.00", String.valueOf(apiShare))) {
                BigDecimal quota = BigDecimalUtils.division(apiShare, 100);
                BigDecimal channelSharePrice = BigDecimalUtils.multiply(phoneMaskField.getPrice(), quota);
                phoneMaskField.setChannelSharePrice(channelSharePrice);
            } else {
                phoneMaskField.setChannelSharePrice(phoneMaskField.getPrice());
            }
        }

        apiReceivePhoneMaskDataExchange.setListPhoneMaskField(listPhoneMaskField);
        return apiReceivePhoneMaskDataExchange;
    }
}
